package listnode;

import listnode.data.ListNode;


/**
 * 旋转链表
 */
public class RotateRight {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) {
            return null;
        }

        int len = 1;
        ListNode cur = head;
        while (cur.next != null) {
            len++;
            cur = cur.next;
        }

        int pos = len - k % len;

        if (pos==len){
            return head;
        }

        //连成环
        cur.next = head;

        while (pos-- > 0) {
            cur = cur.next;
        }

        ListNode ret = cur.next;
        cur.next = null;
        return ret;
    }
}
